# -*- coding: utf-8 -*-
"""
Created on Sat Sep 19 16:19:01 2020

@author: Administrator
"""

'''
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

Example:

Input: 3
Output:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]
'''

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        '''
        20200919--李运辰
        不太熟悉
        '''
        mareix = [[0 for col in range(n)] for row in range(n)]
        value=1
        rows,columns,xlimit,ylimit,size=0,0,0,0,n
        
        while(value<=(size**2)):
            while(columns<n):
                mareix[rows][columns] =value
                value +=1
                columns +=1
            columns -=1
            rows +=1
            
            while(rows<n):
                mareix[rows][columns] =value
                value +=1
                rows +=1
            columns -=1
            rows -=1
            
            while(columns>=ylimit):
                mareix[rows][columns] =value
                value +=1
                columns -=1
            rows -=1
            columns +=1
            
            while(rows>xlimit):
                mareix[rows][columns] =value
                value +=1
                rows -=1
            rows +=1
            columns +=1
            
            n -=1
            xlimit +=1
            ylimit +=1
            
                
            
            
        return mareix
        